/**
 *  登录表单控制器
 */
'use strict'
import tem from './logIn.html';
import $_fn from '~plugins'
class loginInputCtrl {
    constructor($ui,$scope,$rootScope,$timeout,$location,$interval,http) {
        this.$timeout = $timeout
        let strParam = 'logIncodeImg'
        var token
        //定义变量 用于添加class 高亮提醒用户（红色）
        $scope.error = {
            user:false,
            psw:false,
            code:false,
            phone:false,
            phoneCode:false,
            phoneCode1:false,
            resetpsw0:false,
            resetpsw1:false
        }
        //	登录 声明参数 页面ng-model绑定传送数据到后台
        $scope.UnLogIn = {
            username:'',
            password:'',
            changeCode:'',
            token:''
        }
        //找回密码 页面ng-model绑定传送数据到后台
        $scope.findPsw = {
            phone:'',
            changeCode:'',
            token:''
        }
        //请输入手机验证码
        $scope.enterPsw = {
            phone:'',
            codes:''
        }
        //重置密码
        $scope.resetPsw = {
            psw0:'',
            psw1:'',
            phone:''
        }
        //ajax回调函数
        function getCode(result){
            //闭包函数 实现局部传参
            return cloCallback(result,strParam)
        }
        // 回调函数的闭包函数
        function cloCallback(result,str){
            $('.' + str).attr('src',result.data.img)
            //赋值token 重新发送给后端验证
            token = result.data.token
        }
        //btn点击获取验证码并写入img中   angular函数
        $scope.getLogInCode = function($event,params){
            if(params == 'logIncodeImg') {
                strParam = params//更改需要更新图片的节点
                http._post('imageCode.do',getCode)
            }
            if(params == 'findPswImg'){
                strParam = params//更改需要更新图片的节点
                http._post('imageCode.do',getCode)
            }
        }
        // 首次加载html发送ajax请求获取验证码
        $scope.defaultAjax = function(){
            http._post('imageCode.do',getCode)
        }
        //登录、找回密码点击页面内跳转跳转   跳转发送ajax请求获取验证码
        $scope.logInLocationPath = function(params){
            // 回传登录成功模板切换
            $scope.$emit('template', params);
            if(params == 'findPsw'){
                strParam = 'findPswImg'//更改需要更新图片的节点
                http._post('imageCode.do',getCode)
            }
            if(params == 'logIn'){
                strParam = 'logIncodeImg'//更改需要更新图片的节点
                http._post('imageCode.do',getCode)
            }
        }
        //键盘回车时提交表单
        $scope.key = function(e,param){
            var keycode = window.event?e.keyCode:e.which
            if(keycode == 13){								//回车触发
                if(param == 'logIn'){						//登录界面点击提交触发的方法
                    $scope.logInSuccess('logInSuccess')
                }
                if(param == 'find'){						//找回密码
                    $scope.findPswFun('enterPsw')
                }
                if(param == 'enter'){						//找回密码-输入验证码
                    $scope.enterPswFun()
                }
                if(param == 'reset'){						//找回密码-重置密码
                    $scope.resetPswFun()
                }
            }
        }
        //错误提示
        $scope.errorToast = function(text) {
            $ui.toast({text:text,type:'warning',position:'right',appendTo:'.logIn-content'});
        }


        //登录成功修改成功执行方法   angular函数
        $scope.logInSuccess = function(params){
            if( /^\s*$/.test($scope.UnLogIn.username) ){
                $scope.errorToast('用户名不能为空')
                $scope.error.user = true
                return
            }
            if( /^\s*$/.test($scope.UnLogIn.password) ){
                $scope.errorToast('密码不能为空')
                $scope.error.psw = true
                return
            }
            if( /^\s*$/.test($scope.UnLogIn.changeCode) ){
                $scope.errorToast('验证码不能为空')
                $scope.error.code = true
                return
            }

            if(params == 'logInSuccess'){
                $scope.UnLogIn.token = token						//获取token写入对象内
                var param = $scope.UnLogIn
                var _path = 'adminLogin.do'				//后台请求接口
            }
            http.post(_path,param,function(data){
                if(data.errcode == -1){
                    $scope.errorToast(data.errmsg)
                    return
                }
                //登录成功 修改密码 发送ajxa请求
                if(data.errcode > 0) {
                    // 登录、修改密码成功倒数6s跳转
                    //回传登录成功模板切换
                    $scope.$emit('template', params);
                    //将username传给子组件
                    $rootScope.username = data.data.admin.realName;
                    $scope.UnLogIn.username = data.data.admin.realName// 更新用户名到登录成功倒数页面
                    $rootScope.departmentName = data.data.admin.departmentName
                    $scope.$apply()
                    $_fn.setLocal('menu', data.data.webRightList)   //设置权限菜单
                    $_fn.setLocal('userHelp', data.data.admin)	//存入session
                }
            })
        }
    }
}
loginInputCtrl.$inject = ['$ui','$scope','$rootScope','$timeout','$location','$interval','http']
module.exports = angular.module('directive.loginInput', []).directive('loginInput', function() {
    return {
        restrict: 'E',
        replace:true,
        template: tem,
        controller: loginInputCtrl
    }
}).name